package frames;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import person.PersonRecorder;
import constants.Constants;
import controler.FileControler;
import engine.AvartaEngine;

public class OpenPanel extends JPanel {
	private MainFrame mainFrame;
	private JButton openButton, homeButton, deleteButton;
	private AvartaEngine avartaEngine;
	private Vector<PersonRecorder> savedData;
	private Vector<String> personNameArray;
	private JList list;
	private JScrollPane listPanel;
	private EventHandler eventHandler;

	public OpenPanel() {
		// constructor
		eventHandler = new EventHandler();
		personNameArray = new Vector<>();
		list = new JList(personNameArray);

		// attribute
		this.setLayout(null);
		this.setBackground(Constants.PANEL_BACKGROUND_COLOR);
		this.setSize(Constants.PANEL_SIZE_WIDTH, Constants.PANEL_SIZE_HEIGHT);
		
		// load data
		loadData();

		// list panel
		listPanel = new JScrollPane(list);
		listPanel.setBounds(20, 75, 150, 150);
		this.add(listPanel);

		// open button
		openButton = new JButton(new ImageIcon(Constants.NEXT_IMG));
		openButton.setActionCommand("Open");
		openButton.setBounds(200, 125, 30, 30);
		openButton.addActionListener(eventHandler);
		this.add(openButton);
		
		// home button
		homeButton = new JButton(new ImageIcon(Constants.HOME_IMG));
		homeButton.setActionCommand("Home");
		homeButton.setBounds(30, 250, 30, 30);
		homeButton.addActionListener(eventHandler);
		this.add(homeButton);
		
		// delete button
		deleteButton = new JButton(new ImageIcon(Constants.DELETE_IMG));
		deleteButton.setActionCommand("Delete");
		deleteButton.setBounds(171, 195, 30, 30);
		deleteButton.addActionListener(eventHandler);
		this.add(deleteButton);

	}

	public void init(MainFrame mainFrame) {
		this.mainFrame = mainFrame;
	}

	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paint(g);

		g.drawImage(Constants.OPEN_PANEL_IMG, 50, 15, this);
	}

	public void loadData() {
		personNameArray.removeAllElements();
		
		savedData = FileControler.getFileControler().getSaveFile();
		for (PersonRecorder person : savedData) {
			personNameArray.add(person.getPerson().getName());
		}
		
		list.setListData(personNameArray);
	}
	

	private class EventHandler implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getActionCommand() == "Open") {
				PersonRecorder personRecorder = savedData.get(list.getSelectedIndex());
				avartaEngine = AvartaEngine.getAvartaEngine();
				avartaEngine.setAvartaImageArray(personRecorder);
				mainFrame.openAvarta(avartaEngine.getAvartaImage(), personRecorder);
				
				mainFrame.setExercisePanel();
				mainFrame.sendPersonRecorderToExercisePanel(personRecorder, list.getSelectedIndex());
			}else if (e.getActionCommand() == "Home") {
				mainFrame.setMainPanel();
			}else if (e.getActionCommand() == "Delete") {
				FileControler.getFileControler().deleteData(list.getSelectedIndex());
				loadData();
				repaint();
			}

		}

	}

}
